package com.gui.windows.components;


import javax.swing.*;
import org.jdesktop.beansbinding.*;
import org.jdesktop.swingbinding.*;
import org.jdesktop.layout.*;
import com.data.*;
import com.design.*;
import com.gui.*;
import java.awt.event.*;

import javax.swing.event.MouseInputListener;
import java.util.List;
import java.util.ArrayList;

/**
 * Singleton Class that manages the selection panel that gives the component selection utility.
 * */
public class SelectionPanel extends JPanel implements GuiComponents {
	
	//**************************************************************************************************
	//	Constructor.
	//**************************************************************************************************
	
	private static SelectionPanel selectionPanel = null;
	
	private SelectionPanel (String name) {
		super();
		bindingGroup = new BindingGroup();
		createPanel();
//		updateLists();
	}
	
	public static SelectionPanel getSelectionPanel (String name) {
		if (selectionPanel == null) {
			selectionPanel = new SelectionPanel (name);
		}
		return selectionPanel;
	}
	
	//**************************************************************************************************
	//	Atributes.
	//**************************************************************************************************
	
	private static boolean enabled = false;
	
	// the lists that are used by the combo box.
	private List <String> typeList = new ArrayList <String> ();
	private List <String> nameList = new ArrayList <String> ();
	
	private JLabel[] labels;
	private JComboBox type;
	private JComboBox name;
	private JComboBox filter;
	private JCheckBox check;
	
	private BindingGroup bindingGroup;
		
	//**************************************************************************************************
	//	Methods that exports this Class (Public Methods).
	//**************************************************************************************************
	
	public void setEnabled(boolean enabled) {
		super.setEnabled(enabled);
		this.enabled = enabled;
		updateComponentsEnablement();
	}
	
	public Selection getSelection () {
		
		return new Selection();
	}
	
	public void update() {
		updateLists();
		
	}
	
	//**************************************************************************************************
	//	Inner Methods. (Private Methods).
	//**************************************************************************************************
	
	private void updateLists() {
		System.out.println("updateLists");
		// use the id lists to create the string lists used by the com box. 
		idListToStringList();
		typeList.listIterator(0);
		// binds all combo box with their respective list.
		bindAllComboBox();
	}
	
	/**
	 * Creates the selection panel.
	 * */
	private void createPanel() {
		System.out.println("createPanel");
		initComponents();
		// layouts settings. 
		org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
		this.setLayout(layout);
		layout.setAutocreateGaps(true);
		layout.setAutocreateContainerGaps(true);
		// adding components to the spanel layout.
		layout.setHorizontalGroup(layout.createParallelGroup()	
				.add(name)
				.add(type)
				.add(filter)
		);
		layout.setVerticalGroup(layout.createSequentialGroup()
				.add(name)
				.add(type)
				.add(filter)
		);
	}
	
	/**
	 * Init the panel components.
	 * */
	private void initComponents() {
		name = new JComboBox();
		type = new JComboBox();
		filter = new JComboBox();
		check = new JCheckBox();
		labels = new JLabel[3];
		typeList = new ArrayList <String> ();
		nameList = new ArrayList <String> ();
		setComponents();
	}
	
	private void setComponents () {
		name.setEnabled(false);
		type.setEnabled(false);
		filter.setEnabled(false);
		check.setEnabled(false);
		type.addActionListener(new ComBoxActionHandler());
		name.addActionListener(new ComBoxActionHandler());
	}
	
	private void idListToStringList() {
		System.out.println("idListToStringList");
		for (int i = 0 ; i < DesignManager.availableDesignComponentsTypes.size() ; ++i ) {
			typeList.add(DbImporter.getComponentTypeName(DesignManager.availableDesignComponentsTypes.get(i)));
		}
	}
	
	private void bindAllComboBox() {
		System.out.println("bindAllComboBox");
		bindComboBoxWithList(name, nameList);
		bindComboBoxWithList(type, typeList);
		//TODO: Implement this method.
	}
	
	/**
	 * Updates the enablement of the components. It is called by setEnabled.
	 * */
	private void updateComponentsEnablement() {
		for (int i = 0 ; i < this.getComponentCount() ; ++i ) {
			this.getComponent(i).setEnabled(enabled);
		}
	}
	
	/**
	 * Makes the dinamic binding between a graphic component JComboBox and a List <String> component.
	 * @param {@link Jcombobox}
	 * @param {@link List}
	 * */
	private void bindComboBoxWithList(JComboBox cb, List <String> data) {
		System.out.println("bindComboBoxWithList");
		for (int i = 0 ; i < data.size() ; ++i) System.out.println(data.get(i));
		JComboBoxBinding <String, List <String>, JComboBox> comboBoxBinding = SwingBindings.createJComboBoxBinding(AutoBinding.UpdateStrategy.READ, data,cb);
		bindingGroup.addBinding(comboBoxBinding);
		AutoBinding<Object, Object, Object, Object> binding = Bindings.createAutoBinding(AutoBinding.UpdateStrategy.READ, this, ELProperty.create("${currentRecord.countryId}"), cb,BeanProperty.create("selectedItem"));
		bindingGroup.addBinding(binding);
		bindingGroup.bind();
	}
	
	// **************************************************************************************************
	// * Events Handlers.
	// **************************************************************************************************

	private class ComBoxActionHandler implements ActionListener {
		public void actionPerformed (ActionEvent e) {
//			updateLists();
		}
	}
	
	//**************************************************************************************************
	//	Inner Classes.
	//**************************************************************************************************
	
	protected class Selection {
		public byte componentPrefix;
		public int component;
		
	}
	
}
